const util = require('../../utils/util')
Page({
  data: {
    background: ['/lib/images/banner1.png', '/lib/images/banner2.png', '/lib/images/banner3.png'],
    typeList: [
      {
        'name': 'tengyu',
        'url': 'https://mp.weixin.qq.com/s?__biz=MzI0MzQ5NTgxOQ==&mid=2247484021&idx=1&sn=86442fed2d5b2145d4297eb00683ac17&chksm=e96d6491de1aed87b300cb432503bf617c3d950ad5138d6e9d495e94c197c9419acdc5df1b41&token=528259572&lang=zh_CN#rd'
      },
      {
        'name': 'npm',
        'url': 'https://mp.weixin.qq.com/s?__biz=MzI0MzQ5NTgxOQ==&mid=2247483921&idx=1&sn=c81cfe70108251c855b9df018996f7d8&chksm=e96d64f5de1aede396604ecfd730001cfd017937e2e0aeeb5f9784ae8d54064537b643323ef3&token=528259572&lang=zh_CN#rd'
      },
      {
        'name': 'JS',
        'url': 'https://mp.weixin.qq.com/s?__biz=MzI0MzQ5NTgxOQ==&mid=2247483887&idx=1&sn=5b5b3ba5306dc358d9111c1a76b77f14&chksm=e96d670bde1aee1df1328d57ddd66568b71af7a757165ad8995f90a6ae5703e245ec37c69e8a&token=528259572&lang=zh_CN#rd'
      },
      {
        'name': 'LIB',
        'url': 'https://mp.weixin.qq.com/s?__biz=MzI0MzQ5NTgxOQ==&mid=2247483949&idx=1&sn=4c96b828144ef9780ab025d49a666bda&chksm=e96d64c9de1aeddfdc243213f67269ccab034914e85c8d6a1da2aa2c28cbb92fa1d913eadea9&token=528259572&lang=zh_CN#rd'
      },
      {
        'name': 'SVG',
        'url': 'https://m'
      },
      {
        'name': 'VUE',
        'url': 'https://m'
      },
      {
        'name': 'React',
        'url': 'https://m'
      },
      {
        'name': 'd3.js',
        'url': 'https://m'
      },
      {
        'name': 'JSPlumb',
        'url': 'https://mp.weixin.qq.com/s?__biz=MzI0MzQ5NTgxOQ==&mid=2247483994&idx=1&sn=3b152ca87d2d9b097990806eeb40bb86&chksm=e96d64bede1aeda83fef80d50c9e48b9ea5c915fe83f24fd71485a3b68bb8c1977b9a146269d&token=528259572&lang=zh_CN#rd'
      }
    ],
    wenzhangList: [],
    doubanList: [],
    csdnList: [],
    currPage: 1,
    key: '前端'
  },

  onShareAppMessage: function () {
    return {
      title: '前端历程',
      path: '/pages/home/index',
      imageUrl: '/lib/images/banner1.png'
    }
  },

  onReady() {
    this.getWenZhagnData()
    this.getDoubanData()
    this.getcsdnList()
    this.getLocAddress()
  },
  getLocAddress() {
    wx.getLocation({
      isHighAccuracy: true, // 开启地图精准定位
      type: 'gcj02', // 地图类型写这个
      success: (res) => {
        const key = 'KVKBZ-QLHC6-CM5SB-EAE37-OUBZS-AZBHK'
        const getAddressUrl = `https://apis.map.qq.com/ws/geocoder/v1/?location=${res.latitude},${res.longitude}&key=${key}&get_poi=1`;
        wx.request({
          url: getAddressUrl,
          success: ops => {
            const app = getApp();
            app.globalData.addressInfo = {
              address: ops.data.result.ad_info.city,
              addressInfo: ops.data.result.address
            };
          }
        })
      },
    })
  },
  openWX(option) {
    const type = option.currentTarget.dataset.type
    wx.navigateTo({
      url: `/pages/webView/webView?type=${type.name}&url=${encodeURIComponent(type.url)}`,
    })
  },
  getDoubanData() {
    wx.request({
      url: 'https://api.vvhan.com/api/douban',
      success: (res) => {
        this.setData({
          doubanList: res.data.data
        })
      }
    })
  },
  getWenZhagnData() {
    wx.request({
      url: 'https://ku.qingnian8.com/wxList.php?num=5&cid=14',
      success: (res) => {
        res.data.map(item => {
          item.posttime = util.formatTime(new Date(item.posttime*1000))
          if(item.title.length > 12) {
            item.title = `${item.title.slice(0, 12)}...`
          }
          return item
        })
        this.setData({
          wenzhangList: res.data
        })
      }
    })
  },
  // 上拉刷新事件
  onReachBottom: function() {
    this.setData({
      currPage: this.data.currPage + 1
    })
    this.getcsdnList()
  },
  changeInput(e) {
    this.setData({
      key: e.detail.value
    })
  },
  searchKey(e) {
    this.setData({
      csdnList: []
    })
    this.getcsdnList()
  },
  clearSearchKey(e) {
    this.setData({
      key: '前端',
      currPage: 1
    })
    this.getcsdnList()
  },
  searchKey(e) {
    this.setData({
      csdnList: [],
      currPage: 1
    })
    this.getcsdnList()
  },
  getcsdnList() {
    wx.showLoading({
      title: '加载中',
    })
    wx.request({
      url: `https://so.csdn.net/api/v3/search?q=${this.data.key}&t=blog&p=${this.data.currPage}&s=0&tm=0&lv=-1&ft=0&l=&u=&ct=-1&pnt=-1&ry=-1&ss=-1&dct=-1&vco=-1&cc=-1&sc=-1&akt=-1&art=-1&ca=-1&prs=&pre=&ecc=-1&ebc=-1&ia=1&platform=pc`,
      success: (res) => {
        res.data.result_vos.map(item => {
          item.posttime = util.formatTime(new Date(item.create_time/1)).slice(0, 10)
          if(item.title.length > 20) {
            item.title = `${item.title.slice(0, 30)}...`
          }
          return item
        })
        this.setData({
          csdnList: [...this.data.csdnList, ...res.data.result_vos]
        })
      }
    })
    wx.hideLoading()
  }
})
